package com.sanadc.shohdi;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

import javax.swing.event.*;


public class FontPropertiesPanel extends JPanel
{
	protected JList nameList;
	protected JComboBox sizeBox;
	protected JCheckBox boldBox;
	protected JCheckBox italicBox;
	
	protected FontListner listner = null;
	
	public final static int [] fontSizes = {8,10,12,14,18,24,32,48,64};
	
	public FontPropertiesPanel()
	{
		super();
		createComponents();
		buildLayout();
		
	}
	
	
	protected void createComponents()
	{
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		String[] names = ge.getAvailableFontFamilyNames();
		nameList =  new JList(names);
		nameList.setSelectedIndex(0);
		nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		nameList.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent event) {
				// TODO Auto-generated method stub
				handleFontPropertyChange ();
				
			}
		});
		
		
		Integer[] sizes = new Integer[fontSizes.length];
		for (int i=0;i<sizes.length;i++)
		{
			sizes[i] = new Integer(fontSizes[i]);
		}
		
		sizeBox = new JComboBox (sizes);
		sizeBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				handleFontPropertyChange();
			}
		});
		
		boldBox = new JCheckBox("Bold");
		boldBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				handleFontPropertyChange();
			}
		});
		
		italicBox = new JCheckBox("Italic");
		italicBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				handleFontPropertyChange();
			}
		});
		
		
	}
	
	
	protected void buildLayout()
	{
		JLabel label;
		GridBagConstraints gbc = new GridBagConstraints ();
		GridBagLayout gbl = new GridBagLayout();
		setLayout(gbl);
		
		
		gbc.anchor = GridBagConstraints.WEST;
		gbc.insets = new Insets(5, 10, 5, 10);
		
		gbc.gridx = 0;
		
		label = new JLabel ("Name :",JLabel.LEFT);
		gbl.setConstraints(label, gbc);
		add (label);
		
		label = new JLabel ("Size :",JLabel.LEFT);
		gbl.setConstraints(label, gbc);
		add (label);
		
		gbl.setConstraints(boldBox, gbc);
		add (boldBox);
		
		
		gbc.gridx++;
		
		nameList.setVisibleRowCount(3);
		JScrollPane jsp = new JScrollPane(nameList);
		
		gbl.setConstraints(jsp, gbc);
		
		add(jsp);
		
		
		gbl.setConstraints(sizeBox, gbc);
		add (sizeBox);
		
		
		gbl.setConstraints(italicBox, gbc);
		add (italicBox);
		
	}
	
	
	
	
	public void setFontListner(FontListner fl)
	{
		listner = fl;
	}
	
	protected void handleFontPropertyChange()
	{
		listner.fontChanged(getSelectedFont());
	}
	
	
	public Font getSelectedFont()
	{
		String name = (String)(nameList.getSelectedValue());
		int style = 0;
		style += (boldBox.isSelected()? Font.BOLD:0);
		style += (italicBox.isSelected()?Font.ITALIC:0);
		int size = ((Integer)(sizeBox.getSelectedItem())).intValue();
		return new Font(name,style,size);
		
	}
	
	
}